/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bice.backbean;

import com.bice.util.MessageProxy;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;

/**
 *
 * @author sirius
 */
@ManagedBean
@ApplicationScoped
public class MessagesProxyBean implements Serializable {
    
    private Map<Long, List<MessageProxy>> messagesPool = null;
    
    private List<MessageProxy>  getMessagesPool(Long userId) {
        if(messagesPool == null) {
            messagesPool = new HashMap<Long, List<MessageProxy>>();
        }
        return messagesPool.get(userId);
    }
    
    public void addMessage(Long userId, MessageProxy message) {
        List<MessageProxy> messages = getMessagesPool(userId);
        if(messages == null) {
            messages = new ArrayList<MessageProxy>();
            messages.add(message);
            messagesPool.put(userId, messages);
        } else {
            messages.add(message);
        }
    }
    
    public List<MessageProxy> popMessages(Long userId) {
        if(messagesPool != null) {
            return messagesPool.remove(userId);
        }
        return null;
    }
    
}
